## MCT merged with new variables and control group

rm(list = ls())
library(tidyverse)
library(caret)
library(haven)
library(stringr)
library(survey)
library(splines)
library(rlang)
library(lmtest)
library(sandwich)
library(broom)
library(MASS)
library(matrixStats)
set.seed(02138)

# Set working directories
#set your own directory please!

#setwd("~/Dropbox (Harvard University)/Gov_2001_Rep/R Scripts")
#setwd("~/Dropbox (Harvard University)/Gov 2001 Rep Paper/R Scripts")


# Load data
d <- read_dta("Datasets/Downes-Sechser-Appendix-C.dta") %>% rename(country_text_a = cabb_a, country_code_a = ccode_a)

# Other MCT vars of interest
mct_csv <- read_csv("Datasets/MCT/mct_dataset_v09.csv") %>% dplyr::select(mct_code, ccode_a, ccode_b, year, force, target_fatalities, compliance) %>%
  rename(country_code_a = ccode_a)

# Other variables
powers <- readRDS("powers.RDS")
vdem_sub <- readRDS("vdem_subset.RDS")
regime <- readRDS("regime.RDS")
conscription <- readRDS("conscription.RDS")
leadership <- readRDS("leadership.RDS")
polity2 <- readRDS("polity2.RDS")


# Also need to do: dyad (unsure which country merge on) colony - geo data (how use) , chisols (how use), 
#dyad: merge based on the pairings in the MCT dataset? Find the dyad pairings based on cabb_a and cabb_b and include the binary colony variable
# geo: have the colonizer variables for all the states in the MCT dataset (cabb_b and cabb_a)? 
#The theoretical point is to see if colonized states tend to be at the receiving end of threats. Also the variables give us a sense of the duration of their colonial status
#chisols: indicates the party/political affiliation of the leader at the time of the crisis. From this we can tell if their domestic support base
#tends to be more conservative (hawkish/pro-military) or liberal(dovish/pacifist)
#noted this in the Excel sheet too--we'll have to Google at the parties of the countries that we're not familiar with.

d2 <- d %>% mutate(country_text = country_text_a, country_code = country_code_a) %>%
  left_join(., y = dplyr::select(powers, - country_code), by = c("country_text", "year")) %>% 
  left_join(., y = vdem_sub, by = c("country_code" = "COWcode", "year")) %>%    #vdem_sub (previously coded as vdem)
  rename(country_text = country_text.x) %>%
  left_join(., y = dplyr::select(regime, - country_code), by = c("country_text", "year")) %>%
  left_join(., y = dplyr::select(conscription, - country_code), by = c("country_text", "year")) %>%
  left_join(.,y = polity2, by=c("country_text_a"= "scode", year = "year")) %>% rename(demo_a=demo) %>%
  left_join(.,y = polity2, by=c("country_text_a"= "scode", year = "year")) %>% rename(demo_b=demo) %>%

 # left_join(., y = leadership, by = c("country_code", "year")) %>%

  left_join(., y = mct_csv, by = c("mct_code", "country_code_a", "ccode_b", "year")) 
  
   
saveRDS(d2, file = "d2.RDS")










